package com.sinotrans.common.exception;

import java.text.MessageFormat;

import org.springframework.util.StringUtils;

import com.sinotrans.common.util.property.BaseProperties;

/**
 * 业务异常
 * 
 * @author gaofu
 *
 */
public class BusinessException extends AbstractException {

	private static final long serialVersionUID = 8151382366190260685L;

	/**
	 * 异常编码
	 */
	protected String errcode;

	/**
	 * 异常信息
	 */
	protected String errmsg;

	public BusinessException() {
		super();
	}

	/**
	 * 构造函数
	 * 
	 * @param errcode 错误编码
	 */
	public BusinessException(String errcode) {
		this(errcode, "");
	}

	/**
	 * 构造函数
	 * 
	 * @param errcode 错误编码
	 * @param params  错误入参
	 */
	public BusinessException(String errcode, Object... params) {
		super(errcode);
		this.errcode = errcode;
		errmsg = BaseProperties.getMsgValue(errcode);
		if (StringUtils.isEmpty(errmsg)) {
			errmsg = errcode;
		} else if (params != null && params.length > 0) {
			errmsg = MessageFormat.format(errmsg, params);
		}
	}

	@Override
	public String getMessage() {
		return errmsg;
	}

	@Override
	public String toString() {
		return getMessage();
	}

	public String getErrcode() {
		return errcode;
	}

	public void setErrcode(String errcode) {
		this.errcode = errcode;
	}

	public String getErrmsg() {
		return errmsg;
	}

	public void setErrmsg(String errmsg) {
		this.errmsg = errmsg;
	}

}
